import env from "./env";

type methodType = "GET" | "POST" | "DELETE" | "PUT";

interface requestParamsType {
  url: string;
  method: "GET" | "POST" | "DELETE" | "PUT";
  data?: any;
}

export default {
  //数据请求封装
  request<T>(requestParamsObj: requestParamsType): Promise<T> {
    //请求loading
    //url地址拼接
    let url = `${env.pro.baseURL}/${requestParamsObj.url}`;
    //基于promise封装
    return new Promise((resolve, reject) => {
      uni.request({
        url,
        method: requestParamsObj.method,
        data: requestParamsObj.data,
        success(res) {
          console.log(res);
          //根据不同的业务接口状态码，也可单独做处理
          resolve(res.data as T);
        },
        fail(err) {
          reject(err);
        },
      });
    });
  },
};
